# Replication Code: Can (Thin) Populism be manipulated without manipulating Host Ideology? Evidence from a conjoint validation approach
# Script 05: Manuscript Analyses and Figures

# Ensure packages from script_01 are loaded
# Requires the following scripts to have been run prior: script_04

# Note: Figure 1 is screenshot of the conjoint task and requires no code/analysis


###################
# Figure 2 (RQ1/H1)
###################

# Figure 2: Marginal Means for Perceptions of People-Centrism and Anti-Elitism (Binary Outcomes, Sample 1)

# Model for People-Centrism Outcome
out_mm_s1_conjoint_listen <- cj(selected ~ `Anti-elitism`+`People-centrism`+Filler, data = s1_conjoint_listen, id = ~respondent, estimate = "mm")
out_mm_s1_conjoint_listen

# Plot People-Centrism Outcome
plot_out_mm_s1_conjoint_listen <- plot(out_mm_s1_conjoint_listen, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right")+
  geom_vline(xintercept=0.5, colour = "gray50", linetype = 'dashed')+
  ggtitle("Outcome: Perceived People-Centrism (S1)")+
  scale_colour_manual(values=rep("black", length(unique(out_mm_s1_conjoint_listen$feature))),
                      guide=FALSE)+
  guides(colour=FALSE)


# Model for Anti-Elitism Outcome
out_mm_s1_conjoint_crooked <- cj(selected ~ `Anti-elitism`+`People-centrism`+Filler, data = s1_conjoint_crooked, id = ~respondent, estimate = "mm")
out_mm_s1_conjoint_crooked

# Plot Anti-Elitism Outcome
plot_out_mm_s1_conjoint_crooked <- plot(out_mm_s1_conjoint_crooked, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right")+
  geom_vline(xintercept=0.5, colour = "gray50", linetype = 'dashed')+
  ggtitle("Outcome: Perceived Anti-Elitism (S1)")+
  scale_colour_manual(values=rep("black", length(unique(out_mm_s1_conjoint_listen$feature))),
                      guide=FALSE)+
  guides(colour=FALSE) 

# Prepare for combining plots
plot_out_mm_s1_conjoint_crooked <- plot_out_mm_s1_conjoint_crooked+ theme(axis.title.y=element_blank(),
                                                                          axis.text.y=element_blank(),
                                                                          axis.ticks.y=element_blank())

# Create Figure 2
fig2 <- ggarrange(plot_out_mm_s1_conjoint_listen, plot_out_mm_s1_conjoint_crooked, ncol=2) # This uses egg package and easiest way for one y-axis label and same size plots


# Save Figure 2
ggsave("fig2.pdf",
       fig2,
       width = 12,
       height = 6)



###################
# Figure 3 (RQ2/H2)
###################

# Figure 3: Marginal Means for Perceptions of Immigration Stance and Conservatism (Binary Outcomes, Sample 1)

# Model for Immigration Outcome
out_mm_s1_conjoint_immig <- cj(selected ~ `Anti-elitism`+`People-centrism`+Filler, data = s1_conjoint_immig, id = ~respondent, estimate = "mm")
out_mm_s1_conjoint_immig

# Plot Immigration Outcome
plot_out_mm_s1_conjoint_immig <- plot(out_mm_s1_conjoint_immig, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right")+
  geom_vline(xintercept=0.5, colour = "gray50", linetype = 'dashed')+
  ggtitle("Outcome: Perceived Immigration Position (S1)")+
  scale_colour_manual(values=rep("black", length(unique(out_mm_s1_conjoint_immig$feature))),
                      guide=FALSE)+
  guides(colour=FALSE)

# Model for Conservatism Outcome
out_mm_s1_conjoint_cons <- cj(selected ~ `Anti-elitism`+`People-centrism`+Filler, data = s1_conjoint_cons, id = ~respondent, estimate = "mm")
out_mm_s1_conjoint_cons

# Plot Conservatism Outcome
plot_out_mm_s1_conjoint_cons <- plot(out_mm_s1_conjoint_cons, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right")+
  geom_vline(xintercept=0.5, colour = "gray50", linetype = 'dashed')+
  ggtitle("Outcome: Perceived Conservatism (S1)")+
  scale_colour_manual(values=rep("black", length(unique(out_mm_s1_conjoint_cons$feature))),
                      guide=FALSE)+
  guides(colour=FALSE) 

# Prepare for combining plots
plot_out_mm_s1_conjoint_cons <- plot_out_mm_s1_conjoint_cons+ theme(axis.title.y=element_blank(),
                                                                    axis.text.y=element_blank(),
                                                                    axis.ticks.y=element_blank())

# Create Figure 3
fig3 <- ggarrange(plot_out_mm_s1_conjoint_immig, plot_out_mm_s1_conjoint_cons, ncol=2) # This uses egg package and easiest way for one y-axis label and same size plots


# Save Figure 3
ggsave("fig3.pdf",
       fig3,
       width = 12,
       height = 6)





##################
# Analysis: RQ3/H3
##################

# Formal Tests for Interaction by Sample Variable (Sample 1 vs. Sample 2)
cj_anova(combined_conjoint_cons, selected ~ `Anti-elitism`+`People-centrism`+Filler, by = ~Sample)
cj_anova(combined_conjoint_cons_ranks, selected ~ `Anti-elitism`+`People-centrism`+Filler, by = ~Sample)
cj_anova(combined_conjoint_immig, selected ~ `Anti-elitism`+`People-centrism`+Filler, by = ~Sample)
cj_anova(combined_conjoint_immig_ranks, selected ~ `Anti-elitism`+`People-centrism`+Filler, by = ~Sample)

##########
# Figure 4
##########

# Figure 4: Marginal Means and Differences in Marginal Means between Sample 1 (no partisan information) and Sample 2 (partisan information included). 

# Top Panels: Immigration Outcome (Binary)

# Model and Difference in Marginal Means
out_mm_comb_conjoint_immig_single <- cj(na.omit(combined_conjoint_immig), selected ~ `Anti-elitism`+`People-centrism`+Filler, id = ~respondent, estimate = "mm", by = ~Sample)
out_mm_comb_conjoint_immig_single

immig_diff_mms <- cj(na.omit(combined_conjoint_immig), selected ~ `Anti-elitism`+`People-centrism`+Filler, id = ~respondent, estimate = "mm_diff", 
                     by = ~Sample)
immig_diff_mms

# Plot Outcome
plot_out_mm_comb_conjoint_immig_single <- plot(out_mm_comb_conjoint_immig_single, group = "Sample", feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right")+
  geom_vline(xintercept=0.5, colour = "gray50", linetype = 'dashed')+
  ggtitle("Outcome: Perceived Immigration Stance")+
  scale_color_manual(values=c("blue", "grey40"))

# Plot Difference in MM
plot_immig_diff_mms <- plot(immig_diff_mms, feature_headers = FALSE)+
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right")+
  geom_vline(xintercept=0, colour = "gray50", linetype = 'dashed')+
  ggtitle("Difference in MMs")+
  scale_colour_manual(values=rep("black", length(unique(out_mm_s1_conjoint_immig$feature))),
                      guide=FALSE)+
  guides(colour=FALSE) 

# Prepare for combining plots
plot_immig_diff_mms <- plot_immig_diff_mms+ theme(axis.title.y=element_blank(),
                                                  axis.text.y=element_blank(),
                                                  axis.ticks.y=element_blank())



# Bottom Panels: Conservative Outcome (Binary)

# Model and Difference in Marginal Means
out_mm_comb_conjoint_cons_single <- cj(na.omit(combined_conjoint_cons), selected ~ `Anti-elitism`+`People-centrism`+Filler, id = ~respondent, estimate = "mm", by = ~Sample)
out_mm_comb_conjoint_cons_single

cons_diff_mms <- cj(na.omit(combined_conjoint_cons), selected ~ `Anti-elitism`+`People-centrism`+Filler, id = ~respondent, estimate = "mm_diff", 
                    by = ~Sample)

cons_diff_mms

# Plot Outcome
plot_out_mm_comb_conjoint_cons_single <- plot(out_mm_comb_conjoint_cons_single, group = "Sample", feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right")+
  geom_vline(xintercept=0.5, colour = "gray50", linetype = 'dashed')+
  ggtitle("Outcome: Perceived Conservatism")+
  scale_color_manual(values=c("blue", "grey40"))

# Plot Difference in MM
plot_cons_diff_mms <- plot(cons_diff_mms, feature_headers = FALSE)+
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right")+
  geom_vline(xintercept=0, colour = "gray50", linetype = 'dashed')+
  ggtitle("Difference in MMs")+
  scale_colour_manual(values=rep("black", length(unique(out_mm_s1_conjoint_cons$feature))),
                      guide=FALSE)+
  guides(colour=FALSE) 

# Prepare for combining plots
plot_cons_diff_mms <- plot_cons_diff_mms+ theme(axis.title.y=element_blank(),
                                                axis.text.y=element_blank(),
                                                axis.ticks.y=element_blank())

# Create Figure 4
fig4 <- ggarrange(plot_out_mm_comb_conjoint_immig_single, plot_immig_diff_mms, plot_out_mm_comb_conjoint_cons_single, plot_cons_diff_mms, ncol=2)


# Save Figure 4
ggsave("fig4.pdf",
       fig4,
       width = 12,
       height = 9)












